package com.example.exam.controller;

import com.example.exam.entity.ExamineeInfo;
import com.example.exam.req.LoginReq;
import com.example.exam.req.RegisterReq;
import com.example.exam.resp.CommonResp;
import com.example.exam.resp.ExamineeILoginResp;
import com.example.exam.service.impl.ExamineeInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@ResponseBody
@RestController
@RequestMapping("/examinee")
public class ExamineeController {

    @Autowired
    private ExamineeInfoServiceImpl examineeInfoService;

    /**
     * 登录
     * @param loginReq
     * @return
     */
    @PostMapping("/login")
    public CommonResp login(@RequestBody LoginReq loginReq) {
        CommonResp<ExamineeILoginResp> resp = new CommonResp<>();
        ExamineeILoginResp loginResp = new ExamineeILoginResp();
        int num = examineeInfoService.getNumByIDCard(loginReq.getExamineeIDCard());
        if (loginReq.getExamineeIDCard() != null && loginReq.getPwd() != null && num != 0) {
            ExamineeInfo examineeInfo = examineeInfoService.loginByIDCardAndePwd(loginReq.getExamineeIDCard(), loginReq.getPwd());
            loginResp.setExamineeId(examineeInfo.getExamineeId());
            loginResp.setExamineeAddress(examineeInfo.getExamineeAddress());
            loginResp.setExamineeName(examineeInfo.getExamineeName());
            loginResp.setExamineePhone(examineeInfo.getExamineePhone());
            loginResp.setExamineeSex(examineeInfo.getExamineeSex());
            resp.setContent(loginResp);
        } else {
            resp.setSuccess(false);
            resp.setMessage("账户未找到，请重新注册");
        }
        return resp;
    }

    /**
     * 注册
     * @param registerReq
     * @return
     */
    @PostMapping("/register")
    public CommonResp register(@RequestBody RegisterReq registerReq) {
        CommonResp resp = new CommonResp();
        int num = examineeInfoService.getNumByIDCard(registerReq.getExamineeIDCard());
        int count = examineeInfoService.getCountIDCard(registerReq.getExamineeIDCard());
        if (num > 0) {
            resp.setSuccess(false);
            resp.setMessage("该身份证号已经被注册过，请直接登录！");
        } else if (count != 0) {
            examineeInfoService.update(registerReq);
            resp.setMessage("创建成功");
        } else {
            examineeInfoService.register(registerReq);
            resp.setMessage("创建成功");
        }
        return resp;
    }

    /**
     * 注销
     * @param IDCard
     * @return
     */
    @GetMapping("/logout/{IDCard}")
    public CommonResp logout(@PathVariable("IDCard") String IDCard) {
        System.out.println(IDCard);
        CommonResp resp = new CommonResp();
        int count = examineeInfoService.getNumByIDCard(IDCard);
        if (count != 0) {
            examineeInfoService.logoutByIDCard(IDCard);
            resp.setMessage("注销成功");
        } else {
            resp.setSuccess(false);
            resp.setMessage("该账户不存在");
        }
        return resp;
    }

    @PostMapping("/updateExamineeInfo")
    public CommonResp updateExamineeInfo(@RequestBody RegisterReq registerReq) {
        CommonResp resp = new CommonResp();
        boolean flag = examineeInfoService.updateExamineeInfo(registerReq);
        if (flag) {
            resp.setMessage("更新成功");
            return resp;
        }
        resp.setMessage("更新失败");
        return resp;
    }

}
